package com.wenlong.controller;

import com.wenlong.pojo.Commodity;
import com.wenlong.pojo.Page;
import com.wenlong.service.CommodiftyService;
import com.wenlong.service.impl.CommodittyServiceImpl;
import com.wenlong.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 苏文龙
 * @version 1.0
 */
@WebServlet(name = "ClientCommodity" ,urlPatterns = {"/clients/clientCommodity"})
//@SuppressWarnings({"all"})
public class ClientCommodity extends BaseServlet {
    CommodiftyService cs = new CommodittyServiceImpl();
    public void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Commodity> page = cs.page(pageNo,pageSize);
        page.setUrl("clients/clientCommodity?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }

    public void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parseInt(req.getParameter("min"), 0);
        int max = WebUtils.parseInt(req.getParameter("max"), Integer.MAX_VALUE);
        Page<Commodity> page = cs.pageByPrice(pageNo,pageSize,min,max);
        StringBuilder builder = new StringBuilder("clients/clientCommodity?action=pageByPrice");
        if (req.getParameter("min") != null) {
            builder.append("&min=").append(req.getParameter("min"));
        }
        if (req.getParameter("max") != null) {
            builder.append("&max=").append(req.getParameter("max"));
        }
        page.setUrl(builder.toString());
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
}
